package com.ningxia.water.controller.geographic;

import com.ningxia.water.entity.ZtDistrictInfo;
import com.ningxia.water.entity.ZtTownInfo;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.IZtDistrictInfoService;
import com.ningxia.water.utils.CommonUtils;
import com.ningxia.water.vo.out.ZonePidVo;
import com.ningxia.water.vo.out.ZoneVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangzhen
 * @create 2021-05-14 21:03
 */
@RestController
@RequestMapping("/district")
@Api(tags = {"地理信息-2-区县API"})
public class DistrictController {

    @Autowired
    private IZtDistrictInfoService districtInfoService;


    @GetMapping("/getdistrictinfo")
    @ApiOperation("获取全部区县地理信息")
    public Result getDistrictInfo() {
        List<ZoneVo> list = districtInfoService.getDistrictInfos();
        return ResultResponse.getSuccessResult(list);
    }

    @PostMapping("/insertdistrictinfo")
    @ApiOperation(value = "新增区县")
    public Result insertDistrictInfo(@RequestBody ZtDistrictInfo districtInfo) {
        return CommonUtils.commonResult(districtInfoService.save(districtInfo));
    }

    @PutMapping("/updatedistrictinfo")
    @ApiOperation(value = "根据ID更新区县")
    public Result updateDistrictInfo(@RequestBody ZtDistrictInfo districtInfo) {
        return CommonUtils.commonResult(districtInfoService.updateById(districtInfo));
    }


    @DeleteMapping("/deletedistrictinfo")
    @ApiOperation(value = "根据id删除区县")
    public Result deleteDistrictInfo(@RequestParam("districtId") Integer districtId) {
        Integer count = districtInfoService.findByDistrictId(districtId);
        if (count == 0) {
            return CommonUtils.commonResult(districtInfoService.removeById(districtId));
        }
        return ResultResponse.getFailResult("该区县下含有乡镇");
    }

    @GetMapping("/getalltowns")
    @ApiOperation(value = "获取该区县下的所有乡镇")
    public Result getAllTowns(@RequestParam("districtId") Integer districtId){
        List<ZtTownInfo> list = districtInfoService.getAllTown(districtId);
        return ResultResponse.getSuccessResult(list);
    }

    @GetMapping("/getdistrictrelationship")
    @ApiOperation(value = "获取区县及其对应的城市")
    public Result getDistrictRelationship() {
        List<ZonePidVo> list = districtInfoService.getDistrictRelationship();
        return ResultResponse.getSuccessResult(list);
    }
}
